<?php

namespace app\index\controller;

use app\index\model\IndexModel;
use think\Db;
use think\Session;

class Search extends Border
{



    //微博搜索
    public function wbSearch()
    {

        $search = input('post.Search');
//        halt($search);
        if (empty($search)){
            return $this->error('请输入您要查询的关键字');
        }
        $data = Db::field('n.username,p.photo,n.id,n.role,n.id id')
            ->table(['wb_buser' => 'n', 'wb_buserinfo' => 'p'])
            ->where("n.id = p.uid and n.username like '%$search%'")
            ->select();
//        halt($data);
        //查询话题
        $talk = Db::field('name,id')
            ->table('wb_talk')
            ->where("name like '%$search%'")
            ->select();
        //判断是否是商业用户 存商业用户id 和 name
        if (!empty($data)){
            if ($data['0']['role']==2){
                Session::set('sid',$data['0']['id']);
                Session::set('sname',$data['0']['username']);
            }
        }


        if ($data) {
            $id = $data['0']['id'];
//            halt($id);

            $p = Db::field('photo')
                ->table(['wb_buserinfo'])
                ->where('uid = ' . $id)
                ->find();
            $photo = implode("", $p);
            $username = Session::get('username');
            $id_p = Session::get('id');
            $pto = Db::table('wb_buserinfo')->field('photo')->where('uid',$id_p)->find();
//            halt($pto);
            return $this->fetch('/index/search', [
                'data' => $data,
                'style' => $this->style,
                'list' => $this->list,
                'photo' => $photo,
                'username' => $username,
                'id'=> $id,
                'pto'=>$pto
            ]);

        } else if (!empty($talk)){
//            halt(111);

            return $this->fetch('/index/search',[
                'style' => $this->style,
                'list' => $this->list,
                'res' => $this->res,
                'banner' => $this->banner,
                'style' => $this->style,
                'sort' => $this->sort,
                'talk' => $talk,
                'data'=>$data,
                'data1'=>$this->data1
            ]);
        }else{
            return $this->error('没有此人或没有此话题');
        }
    }

    //游客访问
    public function tourists()
    {
         $res = input();
     // halt($res);
        $data = Db::table(['wb_buser'=>'u','wb_buserinfo'=>'f'])
            ->field(['u.username','u.role','f.photo'])
            ->where('u.id = f.uid and u.id='.$res['id'])
            ->select();
        foreach ($data as $k=>$v){
            $uname =$v['username'];
            $pho =$v['photo'];
            $ro =$v['role'];
        }

        $username = $uname;
        $attentId = $res['id'];
        $role = $ro;
        $photo = $pho;
        $attent_id = $attentId;
        $id = Session::get('id');
        if ($role == 2) {
             if ($id == $attentId) {

                return action('index/Buser/index');

            } else {
                $id = Session::get('id');
                $info = Db::table(['wb_buser' => 'b', 'wb_buserinfo' => 'i'])
                    ->field(['i.photo', 'b.username'])
                    ->where('b.id=i.uid and b.id=' . $attentId)
                    ->where('b.role=2')
                    ->find();
                $articles = Db::table(['wb_buser' => 'b', 'wb_articles' => 'a'])
                    ->field(['b.id', 'b.username', 'a.title', 'a.description', 'a.thumbnail', 'a.content', 'a.uid', 'a.status', 'b.role'])
                    ->where('b.id = a.uid  and b.id=' . $attentId)
                    ->where('b.role=2')
                    ->select();
                $bad = Db::table(['wb_buser' => 'b', 'wb_bad' => 'd'])
                    ->field(['b.id', 'b.username', 'd.title', 'd.path', 'd.uid', 'd.link'])
                    ->where('b.id=d.uid and b.role=2 and b.id=' . $attentId)
                    ->select();
                $attent = Db::table('wb_attent a')
                    ->join('wb_buser b', 'a.attent_id=b.id')
                    ->join('wb_buserinfo i', 'i.uid=b.id')
                    ->field(['i.photo', 'b.username','a.attent_id','b.role'])
                    ->where('a.user_id=' . $attentId)
                    ->select();
                $fans = Db::table('wb_fans f')
                    ->join('wb_buser b', 'f.fans_id=b.id')
                    ->join('wb_buserinfo i', 'i.uid=b.id')
                    ->field(['i.photo', 'b.username','f.fans_id','b.role'])
                    ->where('f.user_id=' . $attentId)
                    ->select();
                $message = Db::table('wb_message m')
                    ->join('wb_buser b', 'm.uid=b.id', 'left')
                    ->join('wb_mpicture p', 'p.mid=m.id ', 'left')
                    ->join('wb_buserinfo i','b.id=i.uid','left')
                    ->field(['m.content', 'm.time', 'm.collectnum', 'm.commentnum', 'm.transpondnum', 'm.agreenum', 'm.readnum', 'b.username', 'm.id', 'group_concat(p.path) path','i.photo','m.src_id','b.id uid'])
                    ->where('b.id=' . $attentId)
                    ->where('m.uid='. $attentId)
                    ->order('m.id desc')
                    ->group('m.id')
                    ->select();

                foreach ($message as $k => $v) {

                    if (!empty($v['src_id'])){
    //                halt($v['src_id']);
                        $res1 = Db::field('m.id id,u.username username,photo,content,time,m.id uid')
                            ->table(['wb_message' => 'm', 'wb_buser' => 'u','wb_buserinfo'=>'i'])
                            ->where('m.uid = u.id and  i.uid = u.id  and  m.id = '.$v['src_id'])
                            ->find();
    //                halt($res1);
                        $message[$k]['old'] = $res1;
                        $path = Db::field('path')
                            ->table(['wb_message' => 'm','wb_mpicture'=>'mp'])
                            ->where('m.id = '.$v['src_id'].' and mp.mid = m.id')
                            ->find();
    //                    halt($path);

                        if (!empty($path)){
                            $message[$k]['old']['path'] = $path;
                        }
    //                    halt($path);
                    }

                }
    //            halt($message);
                // halt($attentId+";"+$id);


                if (!empty($id)) {
                    $attentStatus = $this->isAttent($id, $attentId);
                }else{
                    $attentStatus=1;
                }

                // halt($id);
                if (empty($id)) {
                     return view('index@business/index', [
                        'attentStatus' => $attentStatus,
                        'info' => $info,
                        'articles' => $articles,
                        'bad' => $bad,
                        'attent' => $attent,
                        'fans' => $fans,
                        'message' => $message,
                        'attentId' => $attentId,
                        'id'=>$id

                    ]);
                }

                $arr = Db::field('role')
                    ->table(['wb_buser'])
                    ->where('id = ' . $id)
                    ->find();

                $role = implode("", $arr);



                $p = Db::field('i.photo')
                    ->table(['wb_buserinfo i','wb_buser b'])
                    ->where('i.uid = '.$id)
                    ->where('b.id=i.uid')
                    ->where('b.role=2')
                    ->find();
                if (!empty($p)) {            
                    $photo = implode("",$p);
                }
                $user = Session::get('username');

                 $id_p = Session::get('id');
                 $pto = Db::table('wb_buserinfo')->field('photo')->where('uid',$id_p)->find();
                return view('index@business/index', [
                    'attentStatus' => $attentStatus,
                    'info' => $info,
                    'articles' => $articles,
                    'bad' => $bad,
                    'attent' => $attent,
                    'fans' => $fans,
                    'message' => $message,
                    'attentId' => $attentId,
                    'photo'=>$photo,
                    'username'=>$username,
                    'user'=>$user,
                    'id'=>$id,
                    'pto'=>$pto,


                ]);
            }
        } else {
            if ($id == $attent_id) {
                return action('index/Cuser/index');

            } else {

                $id = Session::get('id');

                $info = Db::table(['wb_buser' => 'b', 'wb_buserinfo' => 'i'])
                    ->field(['i.photo', 'b.username'])
                    ->where('b.id=i.uid and b.id='.$attent_id)
                    ->where('b.role=1')
                    ->find();


                $list = Db::field(['b.username', 'b.id', 'p.name', 'p.uid'])
                    ->table(['wb_buser' => 'b', 'wb_photofolder' => 'p'])
                    ->where('b.id= '.$attent_id.' and b.id = p.uid')
                    ->select();

                $message = Db::table('wb_message m')
                    ->join( 'wb_buser b','m.uid=b.id','left')
                    ->join('wb_mpicture p','p.mid=m.id ','left')
                    ->field(['m.content', 'm.time', 'm.collectnum', 'm.commentnum', 'm.transpondnum', 'm.agreenum', 'm.readnum', 'b.username','m.id','group_concat(p.path) path'])
                    ->where('b.id=' . $attent_id)
                    ->where('m.uid=' . $attent_id)
                    ->group('m.id')
                    ->select();

                $at = Db::field('id,title,thumbnail')
                    ->table('wb_articles')
                    ->select();

                $attent = Db::table('wb_attent a')
                    ->join('wb_buser b', 'a.attent_id=b.id')
                    ->join('wb_buserinfo i', 'i.uid=b.id')
                    ->field(['i.photo', 'b.username','b.role','a.attent_id'])
                    ->where('a.user_id=' . $attent_id)
                    ->select();

                $fans = Db::table('wb_fans f')
                    ->join('wb_buser b', 'f.fans_id=b.id')
                    ->join('wb_buserinfo i', 'i.uid=b.id')
                    ->field(['i.photo', 'b.username','b.role','f.fans_id'])
                    ->where('f.user_id=' . $attent_id)
                    ->select();
                if (!empty($id)) {
                    $attentStatus = $this->isAttent($id, $attent_id);
                }else{
                    $attentStatus=1;
                }
                if (empty($id)) {
                    return view('index@cuser/other', [
                        'list' => $list,
                        'username' =>$username,
                        'photo'=>$photo,
                        'message' => $message,
                        'at' => $at,
                        'attent' => $attent,
                        'fans' => $fans,
                        'attent_id' => $attent_id,
                        'attentStatus' => $attentStatus,
                        'info'=>$info,
                        'id'=>$id,
                    ]);
                }
                $p = Db::field('i.photo')
                    ->table(['wb_buserinfo i', 'wb_buser b'])
                    ->where('i.uid = ' . $id)
                    ->where('b.id=i.uid')
                    ->find();
                if($p!=null){
                    $phot = implode("", $p);

                }

                $user = Session::get('username');
                return view('index@cuser/other', [
                    'list' => $list,
                    'user'=>$user,
                    'username' =>$username,
                    'photo'=>$photo,
                    'message' => $message,
                    'at' => $at,
                    'attent' => $attent,
                    'fans' => $fans,
                    'attent_id' => $attent_id,
                    'attentStatus' => $attentStatus,
                    'info'=>$info,
                    'id'=>$id,
                    'phot'=>$phot,
                ]);
            }
        }


    }



    public function isAttent($id, $attentId)
    {
        if(0!=$id) {
            $attent = Db::table(['wb_attent' => 'a'])
                ->field(['a.user_id', 'a.attent_id'])
                ->where('a.user_id=' . $id)
                ->where(' a.attent_id=' . $attentId)
                ->find();
//            halt($attent);
            $fans = Db::table(['wb_fans' => 'f'])
                ->field(['f.user_id', 'f.fans_id'])
                ->where('f.user_id=' . $attentId)
                ->where('fans_id=' . $id)
                ->find();
//            halt($fans);
            $both = Db::table(['wb_attent' => 'a', 'wb_fans' => 'f'])
                ->where('a.user_id=f.user_id and a.attent_id=f.fans_id')
                ->find();
//            halt($both);
            if (null != $attent) {
                if (null != $fans && null != $both) {
                    $attentStatus = 3;
                } else {
                    $attentStatus = 2;
                }
            } else {
                $attentStatus = 1;
            }
        }else{
            $attentStatus = 1;
        }
        return $attentStatus;
    }





}

